﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace Viet_Calculator
{
	/// <summary>
	/// Summary description for SoNguyenTo.
	/// </summary>
	public class SoNguyenTo : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button btnKiemTra;
		private System.Windows.Forms.TextBox txtKiemTra;
		private System.Windows.Forms.TextBox txtSo;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SoNguyenTo()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SoNguyenTo));
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label2 = new System.Windows.Forms.Label();
			this.linkLabel1 = new System.Windows.Forms.LinkLabel();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnKiemTra = new System.Windows.Forms.Button();
			this.txtKiemTra = new System.Windows.Forms.TextBox();
			this.txtSo = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.linkLabel1);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(616, 120);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Hướng dẫn";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 72);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(600, 40);
			this.label2.TabIndex = 2;
			this.label2.Text = "Các bạn hãy điền vào ô trống những giá trị thích hợp nếu các bạn muốn biết giá tr" +
				"ị mà các bạn điền có phải là Số Nguyên Tố hay không";
			// 
			// linkLabel1
			// 
			this.linkLabel1.Location = new System.Drawing.Point(8, 48);
			this.linkLabel1.Name = "linkLabel1";
			this.linkLabel1.Size = new System.Drawing.Size(376, 24);
			this.linkLabel1.TabIndex = 1;
			this.linkLabel1.TabStop = true;
			this.linkLabel1.Text = "http://www.osix.net/modules/article/?id=791";
			this.linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(linkLabel1_LinkClicked);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(600, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Đây là phần mà mình có sử dụng mã nguồn mở của tác giả \"sfabriz\" tại địa chỉ ";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.btnKiemTra);
			this.groupBox2.Controls.Add(this.txtKiemTra);
			this.groupBox2.Controls.Add(this.txtSo);
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.groupBox2.Location = new System.Drawing.Point(8, 144);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(616, 64);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Kiểm tra xem giá trị mà các bạn điền có phải là Số Nguyên Tố hay không";
			// 
			// btnKiemTra
			// 
			this.btnKiemTra.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnKiemTra.Location = new System.Drawing.Point(456, 24);
			this.btnKiemTra.Name = "btnKiemTra";
			this.btnKiemTra.Size = new System.Drawing.Size(144, 24);
			this.btnKiemTra.TabIndex = 2;
			this.btnKiemTra.Text = "Kiểm tra";
			this.btnKiemTra.Click += new EventHandler(btnKiemTra_Click);
			// 
			// txtKiemTra
			// 
			this.txtKiemTra.Location = new System.Drawing.Point(184, 24);
			this.txtKiemTra.Name = "txtKiemTra";
			this.txtKiemTra.ReadOnly = true;
			this.txtKiemTra.Size = new System.Drawing.Size(256, 22);
			this.txtKiemTra.TabIndex = 1;
			this.txtKiemTra.Text = "";
			// 
			// txtSo
			// 
			this.txtSo.Location = new System.Drawing.Point(8, 24);
			this.txtSo.Name = "txtSo";
			this.txtSo.Size = new System.Drawing.Size(160, 22);
			this.txtSo.TabIndex = 0;
			this.txtSo.Text = "";
			// 
			// SoNguyenTo
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(634, 224);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox2);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "SoNguyenTo";
			this.Text = "Lam Viec Voi So Nguyen To";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion



		// Kiểm tra xem giá trị mà các bạn điền có phải là Số Nguyên Tố hay không
		private bool IsPrime(int n) 
		{ 
			if (n==2 || n==3)
			{
				return true; 
			}
			
			if ((n&0x1)==0 || n%3==0 || n<2) 
			{
				return false; 
			}

			long root=(long)System.Math.Sqrt(n)+1L; 
			
			for (long k=6;k<=root;k+=6) 
			{ 
				if (n%(k-1)==0) 
				{
					return false; 
				}
				if (n%(k+1)==0) 
				{
					return false; 
				}
			} 
			return true; 
		}


		private void btnKiemTra_Click(object sender, System.EventArgs e)
		{
			if (txtSo.Text == "")
			{
				MessageBox.Show("Hãy điền vào ô trống 1 giá trị!");
			}
			else
			{
				try
				{
					int n = Convert.ToInt32(txtSo.Text);
					if (IsPrime(n)) 
					{
						txtKiemTra.Text = n + " là Số Nguyên Tố";
					}
					else
					{
						txtKiemTra.Text = n + " không phải là Số Nguyên Tố";
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show("Đã có lỗi xảy ra! Các bạn hãy kiểm tra lại xem mình đã thao tác đúng chưa nhé!", ex.Source);
				}
			}
		}


		private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			Process.Start("http://www.osix.net/modules/article/?id=791");
		} 
	} 
}
